<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    div {
      width: 200px;
      height: 200px;
      background-color: pink;
    }
  </style>
</head>

<body>
  <div>

  </div>
  <script>
    let div = document.getElementsByTagName('div')[0]
    function debounce(fn, delay) {
      // 利用闭包保存定时器
      console.log(this)
      let timer = null
      return function () {
        let context = this
        let arg = arguments
        // 在规定时间内再次触发会先清除定时器后再重设定时器
        clearTimeout(timer)
        timer = setTimeout(function () {
          console.log(this)
          fn.apply(context, arg)
        }, delay)
      }
    }

    function fn() {
      console.log('防抖')
    }
    div.addEventListener('click', debounce(fn, 1000))
  </script>
</body>

</html>